### CUnit Progammers Guide

Prev | Home | Next

## 4. Managing Tests & Suites

In order for a test to be run by CUnit, it must be added to a test collection (suite) which is registered with the test registry.

### 4.1. Synopsis

#include <CUnit/TestDB.h> (included automatically by <CUnit/CUnit.h>)

      typedef struct CU_Suite
      typedef CU_Suite*  CU_pSuite

      typedef struct CU_Test
      typedef CU_Test*  CU_pTest

      typedef void (*CU_TestFunc)(void)
      typedef int  (*CU_InitializeFunc)(void)
      typedef int  (*CU_CleanupFunc)(void)

      CU_pSuite CU_add_suite(const char* strName,
                             CU_InitializeFunc pInit,
                             CU_CleanupFunc pClean);

      CU_pTest  CU_add_test(CU_pSuite pSuite,
                            const char* strName,
                            CU_TestFunc pTestFunc);

      typedef struct CU_TestInfo
      typedef struct CU_SuiteInfo

      CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]);
      CU_ErrorCode CU_register_nsuites(int suite_count, ...);

      CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive)
      CU_ErrorCode CU_set_test_active(CU_pTest, CU_BOOL fNewActive)

      CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName)
      CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit)
      CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean)

      CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName)
      CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc)

      CU_pSuite CU_get_suite(const char* strName)
      CU_pSuite CU_get_suite_at_pos(unsigned int pos)
      unsigned int CU_get_suite_pos(CU_pSuite pSuite)
      unsigned int CU_get_suite_pos_by_name(const char* strName)

      CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName)
      CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos)
      unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest)
      unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName)

### 4.2. Adding Suites to the Registry

CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean)

Creates a new test collection (suite) having the specified name, initialization function, and cleanup function. The new suite is registered with (and owned by) the test registry, so the registry must be initialized before adding any suites. The current implementation does not support the creation of suites independent of the test registry. This function may not be called during a test run (i.e. from a test function or suite initialization/cleanup function).
It is recommended that the suite's name be unique among all suites in the registry. This facilitates suite lookup by name, which only finds the first suite having a given name. The initialization and cleanup functions are optional, and are passed as pointers to functions to be called before and after running the tests contained in the suite. This allows the suite to set up and tear down temporary fixtures to support running the tests. These functions take no arguments and should return zero if they are completed successfully (non-zero otherwise). If a suite does not require one or both of these functions, pass `NULL` to CU_add_suite().
A pointer to the new suite is returned, which is needed for adding tests to the suite. The pointer will be `NULL` if a fatal error occurs. In addition, the framework error code is set to one of the following:

CUE_SUCCESS | suite creation was successful.
CUE_NOREGISTRY | Error: the registry has not been initialized.
CUE_NO_SUITENAME | Error: strName was `NULL`.
CUE_DUP_SUITE | Warning: the suite's name was not unique.
CUE_NOMEMORY | Error: memory allocation failed.

### 4.3. Adding Tests to Suites

CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc)

Creates a new test having the specified name and test function, and registers it with the specified suite. The suite must already have been created using CU_add_suite(). The current implementation does not support the creation of tests independent of a registered suite. This function may not be called during a test run (i.e. from a test function or suite initialization/cleanup function).
It is recommended that the test's name be unique among all tests added to a single suite. This facilitates lookup of the test by name, which will find only the 1st test having a given name. The test function cannot be `NULL`, and points to a function to be called when the test is run. Test functions have neither arguments nor return values.
A pointer to the new test is returned. If a fatal error occurs during creation of the test, `NULL` is returned. In addition. the framework error code is set to one of the following:

CUE_SUCCESS | suite creation was successful.
CUE_NOREGISTRY | Error: the registry has not been initialized.
CUE_NOSUITE | Error: the specified suite was `NULL` or invalid.
CUE_NO_TESTNAME | Error: strName was `NULL`.
CUE_NO_TEST | Error: pTestFunc was `NULL` or invalid.
CUE_DUP_TEST | Warning: the test's name was not unique.
CUE_NOMEMORY | Error: memory allocation failed.

### 4.4. Shortcut Methods for Managing Tests

#define CU_ADD_TEST(suite, test) (CU_add_test(suite, #test, (CU_TestFunc)test))

This macro automatically generates a unique test name based on the test function name, and adds it to the specified suite. The return value should be checked by the user to verify success.

CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[])
CU_ErrorCode CU_register_nsuites(int suite_count, ...)

For large test structures with many tests and suites, managing test/suite associations and registration is tedious and error-prone. CUnit provides a special registration system to help manage suites and tests. Its primary benefit is to centralize the registration of suites and associated tests, and to minimize the amount of error checking code the user needs to write.
Test cases are first grouped into arrays of CU_TestInfo instances (defined in <CUnit/TestDB.h>):

    CU_TestInfo test_array1[] = {
      { "testname1", test_func1 },
      { "testname2", test_func2 },
      { "testname3", test_func3 },
      CU_TEST_INFO_NULL,
    };

Each array element contains the (unique) name and test function for a single test case. The array must end with an element holding `NULL` values, which the macro CU_TEST_INFO_NULL conveniently defines. The test cases included in a single CU_TestInfo array form the set of tests that will be registered with a single test suite.
Suite information is then defined in one or more arrays of CU_SuiteInfo instances (defined in <CUnit/TestDB.h>):

    CU_SuiteInfo suites[] = {
      { "suitename1", suite1_init-func, suite1_cleanup_func, test_array1 },
      { "suitename2", suite2_init-func, suite2_cleanup_func, test_array2 },
      CU_SUITE_INFO_NULL,
    };

Each of these array elements contain the (unique) name, suite initialization function, suite cleanup function, and CU_TestInfo array for a single suite. As usual, `NULL` may be used for the initialization or cleanup function if the given suite does not need it. The array must end with an all-`NULL` element, for which the macro CU_SUITE_INFO_NULL may be used.
All suites defined in a CU_SuiteInfo array can then be registered in a single statement:

`CU_ErrorCode error = CU_register_suites(suites);`

If an error occurs during the registration of any suite or test, an error code is returned. The error codes are the same as those returned by normal suite registration and test addition operations. The function CU_register_nsuites() is provided for situations in which the user wishes to register multiple CU_SuiteInfo arrays in a single statement:

`CU_ErrorCode error = CU_register_nsuites(2, suites1, suites2);`

This function accepts a variable number of CU_SuiteInfo arrays. The first argument indicates the actual number ot arrays being passed.

### 4.5. Activation of Suites and Tests

CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive)
CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive)

These functions may be used to deactivate or reactivate individual tests and suites. A suite or test will not be executed during a test run unless it is active. All suites and tests are active by default upon creation. The current active state is available as pSuite->fActive and pTest->fActive, respectively. The flag will be `CU_TRUE` if active and `CU_FALSE` if not. This gives the client the ability to choose subsets of tests to run dynamically. Note that it is a framework error to deactivate a test or suite and then specifically request that it be run. These functions return `CUE_SUCCESS` on success, and `CUE_NOSUITE` (or `CUE_NOTEST`) if the corresponding suite (or test) is `NULL`.

### 4.6. Modifying Other Attributes of Suites and Tests

Normally the attributes of suites and tests are set at creation time. In some cases, a client may wish to manipulate these to modify the test structure dynamically. The following functions are provided for this purpose, and should be used instead of directly setting the value of the data structure members. All return `CUE_SUCCESS` on success, and the indicated error code on failure. Lookup functions are provided to assist clients in locating specific tests and suites.

CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName)
CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName)

These functions change the name of registered suites and tests. The current names are available as the pSuite->pName and pTest->pName data structure members. If the suite or test is `NULL`, then `CUE_NOSUITE` or `CUE_NOTEST` is returned, respectively. If strNewName is `NULL`, then `CUE_NO_SUITENAME` or `CUE_NO_TESTNAME` is returned, respectively.

CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit)
CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean)

These functions change the initialization and cleanup functions for a registered suite. The current functions are available as the pSuite->pInitializeFunc and pSuite->pCleanupFunc data structure members. If the suite is `NULL` then `CUE_NOSUITE` is returned.

CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc)

This function changes the test function for a registered test. The current test function is available as the pTest->pTestFunc data structure member. If either pTest or pNewFunc is `NULL`, then `CUE_NOTEST` is returned.

### 4.7. Lookup of Individual Suites and Tests

In most cases, clients will have references to registered suites and tests as pointers returned from CU_add_suite() and CU_add_test(). Occassionally, a client may need to be able to retrieve a reference to a suite or test. The following functions are provided to assist clients with this when the client has some information about the entity (name or order of registration). In cases where nothing is known about the suite or test, the client will need to iterate the internal data structures to enumerate the suites and tests. This is not directly supported in the client API.

CU_pSuite CU_get_suite(const char* strName)
CU_pSuite CU_get_suite_at_pos(unsigned int pos)
unsigned int CU_get_suite_pos(CU_pSuite pSuite)
unsigned int CU_get_suite_pos_by_name(const char* strName)

These functions facilitate lookup of suites registered in the active test registry. The first 2 functions allow lookup of the suite by name or position and return `NULL` if the suite cannot be found. The position is a 1-based index in the range [1 .. CU_get_registry()->uiNumberOfSuites]. This may be helpful when suites having duplicate names are registered, in which case lookup by name can only retrieve the 1st suite having that name. The second 2 functions help the client identify the position of a registered suite. These return 0 if the suite cannot be found. In addition, all these functions set the CUnit error state to `CUE_NOREGISTRY` if the registry is not initialized. As appropriate, `CUE_NO_SUITENAME` is set if `strName` is `NULL`, and `CUE_NOSUITE` is set if `pSuite` is `NULL`.

CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName)
CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos)
unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest)
unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName)

These functions facilitate lookup of tests registered in suites. The first 2 functions allow lookup of the test by name or position and return `NULL` if the test cannot found. The position is a 1-based index in the range [1 .. pSuite->uiNumberOfSuites]. This may be helpful when tests having duplicate names are registered, in which case lookup by name can only retrieve the 1st test having that name. The second 2 functions help the client identify the position of a test in a suite. These return 0 if the test cannot be found. In addition, all these functions set the CUnit error state to `CUE_NOREGISTRY` if the registry is not initialized, and to `CUE_NOSUITE` if `pSuite` is `NULL`. As appropriate, `CUE_NO_TESTNAME` is set if `strName` is `NULL`, and `CUE_NOTEST` is set if `pTest` is `NULL`.

### 4.8. Deprecated v1 Data Types & Functions

The following data types and functions are deprecated as of version 2. To use these deprecated names, user code must be compiled with USE_DEPRECATED_CUNIT_NAMES defined.

#include <CUnit/TestDB.h> (included automatically by CUnit/CUnit.h>).

Deprecated Name | Equivalent New Name
`TestFunc` | CU_TestFunc
`InitializeFunc` | CU_InitializeFunc
`CleanupFunc` | CU_CleanupFunc
`_TestCase` | CU_Test
`PTestCase` | CU_pTest
`_TestGroup` | CU_Suite
`PTestGroup` | CU_pSuite
`add_test_group()` | CU_add_suite()
`add_test_case()` | CU_add_test()
`ADD_TEST_TO_GROUP()` | CU_ADD_TEST()
`test_case_t` | CU_TestInfo
`test_group_t` | CU_SuiteInfo
`test_suite_t` | no equivalent - use CU_SuiteInfo
`TEST_CASE_NULL` | CU_TEST_INFO_NULL
`TEST_GROUP_NULL` | CU_SUITE_INFO_NULL
`test_group_register` | CU_register_suites()
`test_suite_register` | no equivalent - use CU_register_suites()

Prev | Home | Next
The Test Registry |  | Running Tests